<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>GraphSAGE &mdash; Graph4NLP v0.4.1 documentation</title><link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  <script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Chapter 5. Decoder" href="../decoding.html" />
    <link rel="prev" title="Graph Attention Networks" href="gat.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> Graph4NLP
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption"><span class="caption-text">Get Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../welcome/installation.html">Install Graph4NLP</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../graphdata.html">Chapter 1. Graph Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset.html">Chapter 2. Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../construction.html">Chapter 3. Graph Construction</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../gnn.html">Chapter 4. Graph Encoder</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="gcn.html">Graph Convolutional Networks</a></li>
<li class="toctree-l2"><a class="reference internal" href="ggnn.html">Gated Graph Neural Networks</a></li>
<li class="toctree-l2"><a class="reference internal" href="gat.html">Graph Attention Networks</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">GraphSAGE</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#graphsage-module-construction-function">GraphSAGE Module Construction Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#graphsage-module-forward-function">GraphSAGE Module Forward Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#graphsagelayer-construction-function">GraphSAGELayer Construction Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#graphsagelayer-forward-function">GraphSAGELayer Forward Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#graphsagelayerconv-construction-function">GraphSAGELayerConv Construction Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#graphsagelayerconv-forwards-function">GraphSAGELayerConv Forwards Function</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../decoding.html">Chapter 5. Decoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../classification.html">Chapter 6. Classification</a></li>
<li class="toctree-l1"><a class="reference internal" href="../evaluation.html">Chapter 7. Evaluations and Loss components</a></li>
</ul>
<p class="caption"><span class="caption-text">Module API references</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../modules/data.html">graph4nlp.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/datasets.html">graph4nlp.datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_construction.html">graph4nlp.graph_construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_embedding.html">graph4nlp.graph_embedding</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/prediction.html">graph4nlp.prediction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/loss.html">graph4nlp.loss</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/evaluation.html">graph4nlp.evaluation</a></li>
</ul>
<p class="caption"><span class="caption-text">Tutorials</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/text_classification.html">Text Classification Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/semantic_parsing.html">Semantic Parsing Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/math_word_problem.html">Math Word Problem Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/knowledge_graph_completion.html">Knowledge Graph Completion Tutorial</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Graph4NLP</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../gnn.html">Chapter 4. Graph Encoder</a> &raquo;</li>
      <li>GraphSAGE</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../../_sources/guide/gnn/graphsage.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <div class="section" id="graphsage">
<span id="guide-graphsage"></span><h1>GraphSAGE<a class="headerlink" href="#graphsage" title="Permalink to this headline">¶</a></h1>
<p>GraphSAGE (<a class="reference external" href="https://arxiv.org/pdf/1706.02216.pdf">GraphSAGE</a>) is a framework for inductive representation learning on large graphs. GraphSAGE is used to generate low-dimensional vector representations for nodes, and is especially useful for graphs that have rich node attribute information. The math operation of GraphSAGE is represented as below:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}h_{\mathcal{N}(i)}^{(l+1)}  = \mathrm{aggregate}\left(\{h_{j}^{l}, \forall j \in \mathcal{N}(i) \}\right)\\h_{i}^{(l+1)}  = \sigma \left(W \cdot \mathrm{concat}(h_{i}^{l}, h_{\mathcal{N}(i)}^{l+1} + b) \right)\\h_{i}^{(l+1)}  = \mathrm{norm}(h_{i}^{l})\end{aligned}\end{align} \]</div>
<p>We provide high level APIs to users to easily define a multi-layer GraphSage model. Besides, we also support both regular GraphSAGE and bidirectional versions including <a class="reference external" href="https://arxiv.org/abs/1808.07624">GraphSAGE-BiSep</a>
and <a class="reference external" href="https://arxiv.org/abs/1908.04942">GraphSAGE BiFuse</a>.</p>
<div class="section" id="graphsage-module-construction-function">
<h2>GraphSAGE Module Construction Function<a class="headerlink" href="#graphsage-module-construction-function" title="Permalink to this headline">¶</a></h2>
<p>The construction function performs the following steps:</p>
<ol class="arabic simple">
<li><p>Set options.</p></li>
<li><p>Register learnable parameters or submodules (<code class="docutils literal notranslate"><span class="pre">GraphSAGELayer</span></code>).</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">GraphSAGE</span><span class="p">(</span><span class="n">GNNBase</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_layers</span><span class="p">,</span> <span class="n">input_size</span><span class="p">,</span> <span class="n">hidden_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">aggregator_type</span><span class="p">,</span> <span class="n">direction_option</span><span class="o">=</span><span class="s1">&#39;undirected&#39;</span><span class="p">,</span> <span class="n">feat_drop</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">norm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">use_edge_weight</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
   <span class="nb">super</span><span class="p">(</span><span class="n">GraphSAGE</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">use_edge_weight</span><span class="o">=</span><span class="n">use_edge_weight</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span> <span class="o">=</span> <span class="n">num_layers</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">=</span> <span class="n">direction_option</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">GraphSAGE_layers</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ModuleList</span><span class="p">()</span>
</pre></div>
</div>
<p>Users can select the number of layers in the GraphSAGE module. If <code class="docutils literal notranslate"><span class="pre">num_layers</span></code> is larger  Than 1, then <code class="docutils literal notranslate"><span class="pre">hidden_size</span></code> should be a list of int values. Based on the values of <code class="docutils literal notranslate"><span class="pre">num_layers</span></code>, we construct the module as</p>
<p>..code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span><span class="o">&gt;</span><span class="mi">1</span><span class="p">:</span>
    <span class="c1"># input projection</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">GraphSAGE_layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">GraphSAGELayer</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span>
                                    <span class="n">hidden_size</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                    <span class="n">aggregator_type</span><span class="p">,</span>
                                    <span class="n">direction_option</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span><span class="p">,</span>
                                    <span class="n">feat_drop</span><span class="o">=</span><span class="n">feat_drop</span><span class="p">,</span>
                                    <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">,</span>
                                    <span class="n">norm</span><span class="o">=</span><span class="n">norm</span><span class="p">,</span>
                                    <span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">))</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">num_layers</span></code> is larger than 1, while the <code class="docutils literal notranslate"><span class="pre">hidden_size</span></code> is an int format value, we assume that all the hidden layers have the same size as</p>
<p>..code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
    <span class="c1"># due to multi-head, the input_size = hidden_size * num_heads</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">GraphSAGE_layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">GraphSAGELayer</span><span class="p">(</span><span class="n">hidden_size</span><span class="p">[</span><span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span>
                                    <span class="n">hidden_size</span><span class="p">[</span><span class="n">l</span><span class="p">],</span>
                                    <span class="n">aggregator_type</span><span class="p">,</span>
                                    <span class="n">direction_option</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span><span class="p">,</span>
                                    <span class="n">feat_drop</span><span class="o">=</span><span class="n">feat_drop</span><span class="p">,</span>
                                    <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">,</span>
                                    <span class="n">norm</span><span class="o">=</span><span class="n">norm</span><span class="p">,</span>
                                    <span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="graphsage-module-forward-function">
<h2>GraphSAGE Module Forward Function<a class="headerlink" href="#graphsage-module-forward-function" title="Permalink to this headline">¶</a></h2>
<p>In NN module, <code class="docutils literal notranslate"><span class="pre">forward()</span></code> function does the actual message passing and computation. <code class="docutils literal notranslate"><span class="pre">forward()</span></code> takes a parameter <code class="docutils literal notranslate"><span class="pre">GraphData</span></code> as input.</p>
<p>The rest of the section takes a deep dive into the <code class="docutils literal notranslate"><span class="pre">forward()</span></code> function.</p>
<p>We first need to obatin the input graph node features and convert the <code class="docutils literal notranslate"><span class="pre">GraphData</span></code> to <code class="docutils literal notranslate"><span class="pre">dgl.DGLGraph</span></code>. Then, we need to determine whether to expand <code class="docutils literal notranslate"><span class="pre">feat</span></code> according to <code class="docutils literal notranslate"><span class="pre">self.use_edge_weight</span></code> and whether to use edge weight according to <code class="docutils literal notranslate"><span class="pre">self.direction_option</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">h</span><span class="o">=</span><span class="n">graph</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_feat&#39;</span><span class="p">]</span> <span class="c1">#get the node feature tensor from graph</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">to_dgl</span><span class="p">()</span> <span class="c1">#transfer the current NLPgraph to DGL graph</span>
<span class="n">edge_weight</span><span class="o">=</span><span class="kc">None</span>
<span class="n">reverse_edge_weight</span><span class="o">=</span><span class="kc">None</span>

<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_edge_weight</span><span class="o">==</span><span class="kc">True</span><span class="p">:</span>
    <span class="n">edge_weight</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">edge_features</span><span class="p">[</span><span class="s1">&#39;edge_weight&#39;</span><span class="p">]</span>
    <span class="n">reverse_edge_weight</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">edge_features</span><span class="p">[</span><span class="s1">&#39;reverse_edge_weight&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>Then we call the low-level GraphSAGE layer to complete the message passing operation. The updated node embedding will be stored back into the node field <code class="docutils literal notranslate"><span class="pre">node_emb</span></code> of GraphData and the final output is the GraphData.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span><span class="o">&gt;</span><span class="mi">1</span><span class="p">:</span>
  <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
      <span class="n">h</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">GraphSAGE_layers</span><span class="p">[</span><span class="n">l</span><span class="p">](</span><span class="n">g</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">edge_weight</span><span class="p">,</span><span class="n">reverse_edge_weight</span><span class="p">)</span>

<span class="n">logits</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">GraphSAGE_layers</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">](</span><span class="n">g</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">edge_weight</span><span class="p">,</span><span class="n">reverse_edge_weight</span><span class="p">)</span>

<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;bi_sep&#39;</span><span class="p">:</span>
    <span class="n">logits</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">(</span><span class="n">logits</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">logits</span> <span class="o">=</span> <span class="n">logits</span>

<span class="n">graph</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_emb&#39;</span><span class="p">]</span><span class="o">=</span><span class="n">logits</span> <span class="c1">#put the results into the NLPGraph</span>
</pre></div>
</div>
</div>
<div class="section" id="graphsagelayer-construction-function">
<h2>GraphSAGELayer Construction Function<a class="headerlink" href="#graphsagelayer-construction-function" title="Permalink to this headline">¶</a></h2>
<p>To make the utilization of GraphSAGE more felxbible, we also provide the low-level implementation of GraphSAGE layer. Below is how to define the <code class="docutils literal notranslate"><span class="pre">GraphSAGELayer</span></code> API.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">GraphSAGELayer</span><span class="p">(</span><span class="n">GNNLayerBase</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">aggregator_type</span><span class="p">,</span> <span class="n">direction_option</span><span class="o">=</span><span class="s1">&#39;undirected&#39;</span><span class="p">,</span> <span class="n">feat_drop</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">norm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">GraphSAGELayer</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
</pre></div>
</div>
<p>Consider we have three options for direction of embeddings, next step is to select the direction type based on <code class="docutils literal notranslate"><span class="pre">direct_option</span></code>. We take the <code class="docutils literal notranslate"><span class="pre">undirected</span></code> as an example.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;undirected&#39;</span><span class="p">:</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">UndirectedGraphSAGELayerConv</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span>
                                <span class="n">output_size</span><span class="p">,</span>
                                <span class="n">aggregator_type</span><span class="p">,</span>
                                <span class="n">feat_drop</span><span class="o">=</span><span class="n">feat_drop</span><span class="p">,</span>
                                <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">,</span>
                                <span class="n">norm</span><span class="o">=</span><span class="n">norm</span><span class="p">,</span>
                                <span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="graphsagelayer-forward-function">
<h2>GraphSAGELayer Forward Function<a class="headerlink" href="#graphsagelayer-forward-function" title="Permalink to this headline">¶</a></h2>
<p>After define a GraphSAGE layer, we can use it to get the node embedding for the input graph. The generated embedding is the output of this layer, as shown in the below example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">graph</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">edge_weight</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">reverse_edge_weight</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">edge_weight</span><span class="p">,</span><span class="n">reverse_edge_weight</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="graphsagelayerconv-construction-function">
<h2>GraphSAGELayerConv Construction Function<a class="headerlink" href="#graphsagelayerconv-construction-function" title="Permalink to this headline">¶</a></h2>
<p>Then let us dive deep to see how the message passing of <code class="docutils literal notranslate"><span class="pre">GraphSAGELayerConv</span></code> for different direction options are implemented.  As an example, we introduce the details of the <code class="docutils literal notranslate"><span class="pre">UndirectedGraphSAGELayerConv</span></code>. The construction function performs the following steps:</p>
<ol class="arabic simple">
<li><p>Set options.</p></li>
<li><p>Register learnable parameters.</p></li>
<li><p>Reset parameters.</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">in_feats</span><span class="p">,</span> <span class="n">out_feats</span><span class="p">,</span> <span class="n">aggregator_type</span><span class="p">,</span> <span class="n">feat_drop</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">norm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">UndirectedGraphSAGELayerConv</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">_in_src_feats</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_in_dst_feats</span> <span class="o">=</span> <span class="n">expand_as_pair</span><span class="p">(</span><span class="n">in_feats</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_out_feats</span> <span class="o">=</span> <span class="n">out_feats</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_aggre_type</span> <span class="o">=</span> <span class="n">aggregator_type</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">norm</span> <span class="o">=</span> <span class="n">norm</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">feat_drop</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="n">feat_drop</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">activation</span>
    <span class="c1"># aggregator type: mean/pool/lstm/gcn</span>
    <span class="k">if</span> <span class="n">aggregator_type</span> <span class="o">==</span> <span class="s1">&#39;pool&#39;</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fc_pool</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_in_src_feats</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_in_src_feats</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">aggregator_type</span> <span class="o">==</span> <span class="s1">&#39;lstm&#39;</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lstm</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">LSTM</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_in_src_feats</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_in_src_feats</span><span class="p">,</span> <span class="n">batch_first</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">aggregator_type</span> <span class="o">!=</span> <span class="s1">&#39;gcn&#39;</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fc_self</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_in_dst_feats</span><span class="p">,</span> <span class="n">out_feats</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">fc_neigh</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_in_src_feats</span><span class="p">,</span> <span class="n">out_feats</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">reset_parameters</span><span class="p">()</span>
</pre></div>
</div>
<p>There are three aggregation types for aggregating the messages passing to each node, namely,  <code class="docutils literal notranslate"><span class="pre">mean</span></code>, <code class="docutils literal notranslate"><span class="pre">pool</span></code>, <code class="docutils literal notranslate"><span class="pre">lstm</span></code>, and <code class="docutils literal notranslate"><span class="pre">gcn</span></code>. And the end of the above code, the parameters are reset.</p>
</div>
<div class="section" id="graphsagelayerconv-forwards-function">
<h2>GraphSAGELayerConv Forwards Function<a class="headerlink" href="#graphsagelayerconv-forwards-function" title="Permalink to this headline">¶</a></h2>
<p>The message passing operation have four options considering four aggregation types. Here we take the <code class="docutils literal notranslate"><span class="pre">list</span></code> type as an example.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_aggre_type</span> <span class="o">==</span> <span class="s1">&#39;lstm&#39;</span><span class="p">:</span>
    <span class="n">graph</span><span class="o">.</span><span class="n">srcdata</span><span class="p">[</span><span class="s1">&#39;h&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">feat_src</span>

    <span class="k">if</span> <span class="n">edge_weight</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">graph</span><span class="o">.</span><span class="n">update_all</span><span class="p">(</span><span class="n">fn</span><span class="o">.</span><span class="n">copy_src</span><span class="p">(</span><span class="s1">&#39;h&#39;</span><span class="p">,</span> <span class="s1">&#39;m&#39;</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lstm_reducer</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
       <span class="n">graph</span><span class="o">.</span><span class="n">edata</span><span class="p">[</span><span class="s1">&#39;edge_weight&#39;</span><span class="p">]</span><span class="o">=</span><span class="n">edge_weight</span>
       <span class="n">graph</span><span class="o">.</span><span class="n">update_all</span><span class="p">(</span><span class="n">fn</span><span class="o">.</span><span class="n">u_mul_e</span><span class="p">(</span><span class="s1">&#39;h&#39;</span><span class="p">,</span> <span class="s1">&#39;edge_weight&#39;</span><span class="p">,</span><span class="s1">&#39;m&#39;</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lstm_reducer</span><span class="p">)</span>
    <span class="n">h_neigh</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">dstdata</span><span class="p">[</span><span class="s1">&#39;neigh&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>We could find that the above implementation also consider the situation of using the <code class="docutils literal notranslate"><span class="pre">edge_weight</span></code>.</p>
<p>After the message passing and aggregation of the messages, we finally update the embedding of nodes and make them as the final outputs as</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_aggre_type</span> <span class="o">==</span> <span class="s1">&#39;gcn&#39;</span><span class="p">:</span>
    <span class="n">rst</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fc_neigh</span><span class="p">(</span><span class="n">h_neigh</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">rst</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fc_self</span><span class="p">(</span><span class="n">h_self</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">fc_neigh</span><span class="p">(</span><span class="n">h_neigh</span><span class="p">)</span>
<span class="c1"># activation</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
    <span class="n">rst</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span><span class="n">rst</span><span class="p">)</span>
<span class="c1"># normalization</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">norm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
    <span class="n">rst</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">rst</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="gat.html" class="btn btn-neutral float-left" title="Graph Attention Networks" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="../decoding.html" class="btn btn-neutral float-right" title="Chapter 5. Decoder" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, Graph4AI Group.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>